package com.rzcoding.controller;

import com.rzcoding.dto.base.ResultBean;
import com.rzcoding.dto.base.ResultConst;
import com.rzcoding.entity.Attribute;
import com.rzcoding.entity.Category;
import com.rzcoding.service.AttributeService;
import com.rzcoding.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttributeService attributeService;

    @GetMapping(value = "/categories" )
    public ResponseEntity findPage(@RequestParam(name = "pagenum",required = false,defaultValue = "1") Integer pageNum,
                                   @RequestParam(name = "pagesize",required = false,defaultValue = "10") Integer pageSize,
                                   @RequestParam(name = "type",required = false,defaultValue = "3") Integer type){
        return new ResponseEntity<>(ResultBean.ok(ResultConst.GET_CATEGORYLIST_SUCC, categoryService.findPage(pageNum, pageSize,type)),
                HttpStatus.OK);
    }

    @PostMapping("/categories")
    public ResponseEntity add(Category category){
        categoryService.add(category);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.ADD_CATEGORY_SUCC), HttpStatus.OK);
    }

    @GetMapping("/categories/{id}")
    public ResponseEntity findById(@PathVariable Integer id){
        Category category = categoryService.findById(id);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.GET_CATEGORY_SUCC, category), HttpStatus.OK);
    }

    @PutMapping(value="/categories/{id}")
    public ResponseEntity update(Category category, @PathVariable Integer id){
        category.setId(id);
        categoryService.update(category);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.UPD_CATEGORY_SUCC), HttpStatus.OK);
    }

    @DeleteMapping(value = "/categories/{id}" )
    public ResponseEntity delete(@PathVariable Integer id){
        categoryService.delete(id);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.DEL_CATEGORY_SUCC), HttpStatus.OK);
    }





    @GetMapping("/categories/{id}/attributes")
    public ResponseEntity findAttrByIdSel(@PathVariable Integer id,
                                          @RequestParam(name = "sel") String sel){
        List<Attribute> attrs = attributeService.findAttrByIdSel(id, sel);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.GET_ATTRIBUTELIST_SUCC, attrs),
                HttpStatus.OK);
    }

    @PostMapping("/categories/{catId}/attributes")
    public ResponseEntity addAttr(Attribute attribute, @PathVariable Integer catId){
        attribute.setCatId(catId);
        attributeService.add(attribute);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.ADD_ATTRIBUTE_SUCC),
                HttpStatus.OK);
    }

    @DeleteMapping(value = "/categories/{catId}/attributes/{attrId}" )
    public ResponseEntity deleteAttr(@PathVariable Integer catId, @PathVariable Integer attrId){
        attributeService.delete(attrId);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.DEL_ATTRIBUTE_SUCC),
                HttpStatus.OK);
    }

    @GetMapping(value = "/categories/{catId}/attributes/{attrId}" )
    public ResponseEntity getAttrById(@PathVariable Integer catId, @PathVariable Integer attrId){
        Attribute attribute = attributeService.findById(attrId);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.GET_ATTRIBUTE_SUCC, attribute),
                HttpStatus.OK);
    }

    @PutMapping(value = "/categories/{catId}/attributes/{attrId}" )
    public ResponseEntity updateAttr(Attribute attribute, @PathVariable Integer catId, @PathVariable Integer attrId){
        attribute.setId(attrId);
        attribute.setCatId(catId);
        attributeService.update(attribute);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.UPD_ATTRIBUTE_SUCC),
                HttpStatus.OK);
    }
}
